var app = getApp(), u = app.require("utils/util.js"), a = app.require("utils/api.js"), o = app.require("utils/onfire.js"), h = app.require("utils/http.js"), n = app.require("utils/enums");

// pages/mine/coupon/coupon.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabsNames:[
            { id: 0, name: '未使用', count: 0,type:'unuse' },
            { id: 1, name: '使用记录', count: 0,type:'use' },
            { id: 2, name: '未领取', count: 0,type:'unget' }
        ],
        grouplist:[],
        pageIndex:1,
        currecttype:'unuse',
        grouplistbottom:0,
        userInfo:{},
        emtyTxt:'一张券都没有这么豪的吗？'
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.userInfo = wx.getStorageSync('userInfo');
        this.listcount();
        this.tabsinit(this.data.currecttype);
    },
    listcount:function(){
        var t = this;
        var data = {
            store_id: a.storeId,
            uid:t.data.userInfo.uid,
        };
        h.postReq(a.userCouponListCount,data,function(res){
            if(res.code == 0){
                t.setData({
                    tabsNames: [
                        { id:0, name: '未使用', count: res.data.count.unuse_count,type:'unuse'},
                        { id:1, name: '使用记录', count:res.data.count.use_count,type:'use'},
                        { id:2, name: '未领取', count:res.data.count.unget_count,type:'unget'}
                    ],
                });
            }
        });
    },
    searchProduct:function(e){
        wx.navigateTo({
            url: '../../category/index/list',
        })
    },
    tabsinit: function (typename) {
        var type = "";
        switch (typename) {
            case 'use':
                type = 'use';
                break;
            case 'unget':
                type = 'unget';
                break;
            default:
                type = "unuse";
                break;
        }
        var t = this;
        var data = {
            store_id: a.storeId,
            uid: t.data.userInfo.uid,
            type: type,
            page: t.data.pageIndex
        };
        if (t.data.grouplistbottom === 0) {
            h.postReq(a.userCouponList, data, function (res) {
                if (res.code == 0) {
                    if(res.data.coupon_list.length === 0){
                        t.setData({
                            grouplistbottom:1,
                        });
                    }
                    t.setData({
                        grouplist: t.data.grouplist.concat(res.data.coupon_list),
                        pageIndex: t.data.pageIndex + 1
                    });
                }
            });
        }
    }
    ,
    // tab切换
    switchTab: function (e) {
        var index = e.currentTarget.dataset.index;
        console.log(e.currentTarget.dataset.type);
        this.setData({
            currecttype:e.currentTarget.dataset.type,
            pageIndex:1,
            grouplistbottom:0,
            grouplist:[]
        });
        this.tabsinit(this.data.currecttype);
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function (e) {
        this.tabsinit(this.data.currectIndex);
    }
})